/**
 * [备份恢复-备份]
 * @author lu.hong 2016-09-20
 * @param  {[type]} $scope            [description]
 * @param  {[type]} $http             [description]
 * @param  {[type]} serverService     [description]
 * @param  {[type]} graphService      [description]
 * @param  {[type]} i18nService       [description]
 * @param  {[type]} dialogService     [description]
 * @param  {[type]} asciiTableService [description]
 * @param  {[type]} codeEditorService [description]
 * @param  {[type]} ngNotify          [description]
 * @param  {[type]} $compile)         {	}         [description]
 * @return {[type]}                   [description]
 */
angular.module('app').controller('backupController', ['$scope','$filter','$log','$http','serverService','graphService','i18nService','dialogService','asciiTableService','codeEditorService','ngNotify','$compile','NgTableParams',
  function($scope,$filter,$log,$http,serverService,graphService,i18nService,dialogService,asciiTableService,codeEditorService,ngNotify,$compile,NgTableParams) {

    $scope.refushBackup = function(){
        getBackupData();
    };

    getBackupData();
    //取备份数据
    function getBackupData(){
      //var url = serverService.settings.endpoint.getBackupData;
      var url = "js/data.json";
      var data = {}
      serverService.sendPost(url,data,function(results){
          var ajaxData = [];
          $log.log("发送请求");
          //var results = {"results":[{"columns":["n"],"data":[{"row":[{"port":6362,"host":"127.0.0.1","to":"to3","timeout":1000,"target":"C:\\Users\\intple\\Documents\\Neo4j\\backup"}],"meta":[{"id":560,"type":"node","deleted":false}]}]}],"errors":[]};
          var results = results;
          //console.log(results.results[0].data);
          $.each(results.results[0].data,function(index,data){
              var id = data.meta[0].id;
              var type = data.meta[0].type;
              var host = data.row[0].host;//备份地址
              var port = data.row[0].port;//备份端口
              var to = data.row[0].to;//备份文件名
              var target = data.row[0].target;//备份路径
              var timeout = data.row[0].timeout;//超时时间
              var rsdata = {
                "id":id,
                "type":type,
                "host":host,
                "port":port,
                "to":to,
                "target":target,
                "timeout":timeout
              };
              ajaxData.push(rsdata);
          });
          $log.log(ajaxData);
          $scope.customConfigParams = createUsingFullOptions(ajaxData);
      })
    };

    
    function createUsingFullOptions(ajaxData) {
      var initialParams = {
        page: 1,
        count: 10 // initial page size
      };
      var initialSettings = {
        total: ajaxData.length,
        // page size buttons (right set of buttons in demo)
        //counts: [5,10,15],
        counts: [],
        // determines the pager buttons (left set of buttons in demo)
        paginationMaxBlocks: 13,
        paginationMinBlocks: 2,
        //dataset: $scope.backupdata
        getData: function (params) {
            console.log(ajaxData.length);
            $scope.results = params.sorting() ? $filter('orderBy')(ajaxData, params.orderBy()) : ajaxData;
            $scope.results = $scope.results.slice((params.page() - 1) * params.count(), params.page() * params.count());
            return $scope.results;
        }
      };
      return new NgTableParams(initialParams, initialSettings);
    }


    //创建备份
    $scope.creatBackup = function(){
      var showmodeldata = {
        title:"创建备份",
        message:"",
        size:"SIZE_SMALL",//SIZE_NORMAL,SIZE_SMALL,SIZE_WIDE,SIZE_LARGE
        ngdata:$scope,
        url:'templates/recovery/creatbackup.html',
        buttons:[{
                      label: '取消',
                      cssClass: 'btn-default',
                      action: function(dialogItself){
                        dialogItself.close();
                      }
                  }, {
                      label: '创建',
                      cssClass: 'btn-primary',
                      action: function(dialogItself){
                            var url = serverService.settings.endpoint.backup;
                            var data = {
                               "timeout":$scope.c_outtime_input,
                               "to":$scope.c_backup_input
                            }
                            serverService.sendPost(url,data,function(){
                              dialogItself.close();
                              ngNotify.set('创建成功', 'default');
                              getBackupData();
                            },function(data,header,config,status){
                              dialogItself.close();
                              var errorinfo = data.errors[0].message;
                              ngNotify.set(errorinfo, {type: 'error',sticky: true});
                            });
                      }
                  }
        ]
      };
      dialogService.showmodel(showmodeldata);
    };
    //删除备份
    $scope.deleteBackup = function(){
        var id = $("#backupTable input[name='backup']:checked").val();
        if(!id){
            ngNotify.set('请先选择删除对象', 'warn');
            return false;
        }
        var url = serverService.settings.endpoint.delbackup + id;
        serverService.sendDelete(url,function(){
          dialogItself.close();
          ngNotify.set('删除成功', 'default');
          getBackupData();
        },function(data,header,config,status){
          dialogItself.close();
          var errorinfo = data.errors[0].message;
          ngNotify.set(errorinfo, {type: 'error',sticky: true});
        });
    };
}]);